#include "../include/db_connection_pool.h"
#include <iostream>
#include <thread>

using namespace std;

static db_connection_pool *db_pool = db_connection_pool::GetInstance();

static int cnt = 0;

void test()
{
    sql::Connection *conn = db_pool->GetConnection();

    cout << "Free Connections: " << db_pool->GetFreeConn() << endl;

    sql::Statement *stm = conn->createStatement();
    sql::ResultSet *res = stm->executeQuery("select * from student;");

    while (res->next())
    {
        cout << res->getString("id") << " | ";
        cout << res->getString("name") << " | ";
        cout << res->getString("sex") << " | ";
        cout << res->getString("deptId") << endl;
    }
    cnt++;

    delete res;
    delete stm;
    db_pool->ReleaseConnect(conn);
}

int main()
{
    for (int i = 0; i < 10; ++i)
    {
        thread t(test);
        t.join();
    }
    cout << "\nCount: " << cnt << endl;
    delete db_pool;
    return 0;
}
